/**
 * @file 剪贴板相关
 */

/**
 * 复制文本数据到剪贴板
 * @param {string} text
 * @returns {Promise<void>} 返回一个promise，resolve即为成功
 */
export const copyTextToClipboard = (text: string) => {
  return new Promise<void>((resolve, reject) => {
    let url = text
    var textarea = document.createElement('textarea')
    var currentFocus = document.activeElement as HTMLInputElement
    document.body.appendChild(textarea)
    textarea.value = url
    textarea.focus()
    if (textarea.setSelectionRange) {
      textarea.setSelectionRange(0, textarea.value.length)
    } else {
      textarea.select()
    }
    try {
      document.execCommand('copy')
      resolve()
    } catch (err) {
      reject()
    }
    document.body.removeChild(textarea)
    currentFocus.focus()
  })
}
